      REM Latest Valley conversion by FGC!
      REM started 2/8/04
      REM typing finished 12/8/04
      REM Instructions finally added 15/10/09
      *ESC OFF
      MODE 9:REM 40 X 25, 16 COLS
      VDU 23,1,0;0;0;0; : REM Disable cursor
      PROCload_prefs
      PROCinit
      REPEAT
        PROCmain_menu
        random=RND(-TIME)
        REPEAT
          ended=FALSE
          PROCmove
          IF ended=2 THEN PROCdeath
        UNTIL ended:REM 1=QUIT,2=DEAD
      UNTIL FALSE
      REM-----------------------------------------------------------------------
      
      DEFPROCmove
      con=con+10:old_put=put
      IF what_was=226 OR what_was=227 THEN
        PRINTTAB(0,mess_y);"Safe on the path...Which way ?"
      ELSE
        IF what_was=236 AND bain=TRUE THEN
          PRINTTAB(0,mess_y);"Safe on a tuft...Which way ?"
        ELSE
          PRINTTAB(0,mess_y);"Your move...Which direction ?"
        ENDIF
      ENDIF
      *FX15,0
      rating=FNcalc_rating
      REPEAT
        got$=GET$
      UNTIL INSTR(valid_key$,got$)
      IF FNupc(got$)="E" OR FNupc(got$)="R" THEN PROCrating:ENDPROC
      IF FNupc(got$)="D" THEN PROCdantor:ENDPROC
      IF INSTR("123456789",got$) THEN PROCnum_pad
      turns=turns+1:PRINTTAB(0,mess_y);SPC(39);
      
      put=231:what_next=FNpeek(next_x,next_y):check=FALSE
      IF next_x=curr_x AND next_y=curr_y THEN what_next=what_was
      
      CASE what_next OF
        WHEN 32, 236 : check=TRUE
        WHEN 225 : PROCsafe_castle
        WHEN 224, 238, 233 :turns=turns-1:next_x=curr_x:next_y=curr_y
        WHEN 228, 230, 229,235: PROCscene_control
        WHEN 232, 240: PROCscene_exit
        WHEN 239 : PROCstairs
        WHEN 234 : put=237: con=con-20: check=TRUE: IF con<=0 THEN ended=2
        WHEN 241 : PROCspecial_find
      ENDCASE
      IF got$="5" AND what_was=234 THEN put=237: con=con-20: check=TRUE: IF con<=0 THEN ended=2
      IF hyman=FALSE THEN
        PROCpoke(curr_x,curr_y,what_was):what_was=FNpeek(next_x,next_y)
        put=231
        IF what_was=234 THEN put=237
        curr_x=next_x:curr_y=next_y:PROCpoke(curr_x,curr_y,put)
      ENDIF
      
      IF what_next=226 OR what_next=227 THEN check=FALSE
      IF what_next=236 AND bain=TRUE THEN check=FALSE
      wait=5
      
      IF check THEN
        random=RND(1)
        IF random<0.33 THEN
          PROCmeet_monster
        ELSE
          IF random>0.75 THEN
            PROCfind_select
            wait=80
          ELSE
            PRINT TAB(0,mess_y);"Nothing of value...Search on":wait=80
          ENDIF
        ENDIF
      ENDIF
      IF NOT ended THEN
        IF hyman=TRUE THEN
          PROCpoke(curr_x,curr_y,what_was):what_was=FNpeek(next_x,next_y)
          put=231
          IF what_was=234 THEN put=237
          curr_x=next_x:curr_y=next_y:PROCpoke(curr_x,curr_y,put)
        ENDIF
        PROCdelay(wait,"U")
      ENDIF
      ENDPROC
      
      
      DEFPROCnum_pad
      got=VAL(got$)
      CASE got OF
        WHEN 1 :next_x=curr_x-1:next_y=curr_y+1
        WHEN 2 :next_x=curr_x:next_y=curr_y+1
        WHEN 3 :next_x=curr_x+1:next_y=curr_y+1
        WHEN 4 :next_x=curr_x-1:next_y=curr_y
        WHEN 5 :next_x=curr_x:next_y=curr_y
        WHEN 6 :next_x=curr_x+1:next_y=curr_y
        WHEN 7 :next_x=curr_x-1:next_y=curr_y-1
        WHEN 8 :next_x=curr_x:next_y=curr_y-1
        WHEN 9 :next_x=curr_x+1:next_y=curr_y-1
      ENDCASE
      ENDPROC
      
      
      
      DEFPROCfind_select
      random=RND(6)
      CASE random OF
        WHEN 1 :PRINTTAB(0,mess_y);"A circle of evil...Depart in haste !":com_str=com_str+INT((floor+1)/2):psi=psi-INT((floor+1)/2)
          IF bain=TRUE THEN com_str=com_str-floor-1
          con=con-20:IF con<=0 THEN ended=2
        WHEN 2,3 :PRINTTAB(0,mess_y);"A hoard of gold":treasure=treasure+INT(floor*RND(1)*100+100)
        WHEN 4,5 :PRINTTAB(0,mess_y);"You feel the aura of the deep magic..."'"        ...All around you..."
          psi=psi+2+INT(floor*psi_gain):com_str=com_str+1+INT(floor*com_gain):con=con+25
        WHEN 6 :PRINTTAB(0,mess_y);"...A place of ancient power..."
          psi=psi+2+INT(floor*psi_gain):com_str=com_str+1+INT(floor*com_gain):con=con+25
      ENDCASE
      wait=80
      ENDPROC
      
      DEFPROCspecial_find
      PROCpoke(curr_x,curr_y,32):curr_x=next_x:curr_y=next_y:what_was=32:PROCpoke(curr_x,curr_y,231)
      random=RND(1):PRINTTAB(0,mess_y);SPC(39):special=FALSE
      IF scene=6 AND random>0.95 AND item(1)=6 AND item(2)=0 AND rating>25 THEN
        item(2)=1:PRINTTAB(0,mess_y);"You find the Helm of Evanna !"
        treasure=treasure+100*(item(0)+item(1)+item(2)+floor):special=TRUE
      ENDIF
      IF scene=5 AND random>0.85 AND item(0)=0 THEN
        item(0)=1:PRINTTAB(0,mess_y);"The Amulet of Alarian...Empty..."
        treasure=treasure+100*(item(0)+item(1)+item(2)+floor):special=TRUE
      ENDIF
      
      IF scene=5 AND random>0.7 AND item(0)=1 AND item(3)=0 AND williams=TRUE THEN
        item(3)=1:PRINTTAB(0,mess_y);"You find the Ring of Power !""
        treasure=treasure+100*(item(0)+item(1)+item(2)+floor):special=TRUE
      ENDIF
      
      IF scene=4 AND random>0.7 AND item(0)=1 AND item(1)<6 AND floor>item(1) THEN
        PRINTTAB(0,mess_y);"An amulet stone...":PROCdelay(60,"D"):special=TRUE
        IF random>0.85 THEN
          PRINT'"...But the wrong one !"
        ELSE
          PRINT'"...The stone fits !":item(1)=item(1)+1
          treasure=treasure+100*(item(0)+item(1)+item(2)+floor)
        ENDIF
      ENDIF
      IF special=FALSE THEN
        IF random>0.42 THEN
          PRINTTAB(0,mess_y);"A worthless bauble"
        ELSE
          PRINTTAB(0,mess_y);"A precious stone !"
          treasure=treasure+100*(item(0)+item(1)+item(2)+floor)
        ENDIF
      ENDIF
      PROCdelay(80,"U")
      ENDPROC
      
      DEFPROCmeet_monster
      PRINTTAB(0,mess_y);"** Beware...Thou hast encountered **"
      mon_str=0:mon_psi=0:fighting=1:defeated=0
      REPEAT
        ok=TRUE
        random=RND(17)-1
        IF random>9 AND RND(1)>0.85 THEN ok=FALSE
        IF random=16 AND RND(1)<0.7 THEN ok=FALSE
        IF floor<5 AND random=15 THEN ok=FALSE
        IF what_next=234 THEN random=RND(2)+16
        mon_type$=LEFT$(monster$(random),1)
        IF INSTR(legal_mon$,mon_type$)=0 THEN ok=FALSE
      UNTIL ok
      mon$=MID$(monster$(random),2)
      IF mon_str(random)<>0 THEN mon_str=INT((com_str*0.3)+mon_str(random)*floor^0.2/(RND(1)+1))
      IF mon_psi(random)<>0 THEN mon_psi=INT(mon_psi(random)*floor^0.2/(RND(1)+1))
      exp_gain=INT((random+1)*(floor^1.5))
      IF random>23 THEN exp_gain=INT((random-22)*floor^1.5)
      PRINT'SPC(12-(LEN(mon$)/2));"an evil ";mon$
      IF hyman=TRUE THEN
        under_mon=FNpeek(next_x,next_y)
        PROCpoke(next_x,next_y,242+VAL(MID$(mon_graphic$,random+1,1)))
      ENDIF
      
      PROCdelay(80,"U")
      exp_factor=39*LOG(exp)/3.14
      IF item(3)=1 AND mon_psi<25 THEN
        PRINTTAB(0,mess_y);"The Ring hides you...":PROCdelay(60,"D")
        PROCsurprise
      ELSE
        IF RND(1)<0.6 THEN PROCsurprise
      ENDIF
      IF fighting=1 THEN PROCcombat
      PROCdelay(5,"U")
      ENDPROC
      
      
      DEFPROCsurprise
      PRINTTAB(0,mess_y);"You have surprise...Attack or Retreat"
      PROCcom_get
      IF got$="R" THEN
        PRINTTAB(0,mess_y);"Knavish coward !":fighting=0:wait=5:PROCdelay(80,"D")
        IF hyman=TRUE THEN
          wait=80:PRINTTAB(0,19);SPC(39);:PROCpoke(next_x,next_y,under_mon)
          next_x=curr_x:next_y=curr_y
        ELSE
          wait=5:PROCdelay(80,"W")
        ENDIF
      ENDIF
      IF got$="A" THEN PROCchar_attack
      IF got$="" THEN PRINTTAB(0,mess_y);"* Too slow...Too slow *":PROCdelay(80,"D")
      ENDPROC
      
      DEFPROCcombat
      REPEAT
        IF defeated=0 THEN PROCmon_attack
        IF fighting THEN PROCchar_attack
      UNTIL fighting=0:PRINTTAB(0,19);SPC(39);
      ENDPROC
      
      DEFPROCchar_attack
      PROCdelay(30,"D")
      PRINTTAB(0,mess_y);"*** Strike quickly ***"
      PROCcom_get
      
      IF got$=CHR$(255) THEN
        PRINTTAB(0,mess_y);"* Too slow...Too slow *"
        defeated=0
        PROCdelay(110,"U")
        ENDPROC
      ENDIF
      
      IF got$="S" THEN
        PROCspell_control
        ENDPROC
      ENDIF
      IF mon_str=0 AND got$<>"S" THEN
        PRINTTAB(0,mess_y);"Your sword avails you nought here"
        PROCdelay(110,"U")
        ENDPROC
      ENDIF
      con=con-1
      IF con<20 AND item(3)=1 THEN
        PRINTTAB(0,mess_y);"The Ring slips off your finger...":PROCdelay(110,"D")
        item(3)=0
      ENDIF
      IF con<=0 THEN
        PRINTTAB(0,mess_y);"You fatally exhaust yourself":PROCdelay(110,"D")
        fighting=0
        ended=2
        ENDPROC
      ENDIF
      random=RND(10)-1
      damage_factor=0
      IF got$="H" AND (random<5 OR com_str>mon_str*4) THEN damage_factor=2
      IF got$="B" AND (random<7 OR com_str>mon_str*4) THEN damage_factor=1
      IF got$="L" AND (random<9 OR com_str>mon_str*4) THEN damage_factor=0.3
      IF damage_factor=0 THEN
        PRINTTAB(0,mess_y);"You missed it !"
        defeated=0
        PROCdelay(110,"U")
        ENDPROC
      ENDIF
      IF defeated=1 THEN
        damage=mon_str+RND(9)-1:defeated=0
      ELSE
        damage=INT((((com_str*50*RND(1))-(10*mon_str)+exp_factor)/100)*damage_factor)
        IF damage<0 THEN damage=0
        IF com_str>(mon_str-damage)*4 THEN defeated=1
      ENDIF
      mon_str=mon_str-damage
      PRINTTAB(0,mess_y);"A hit...";
      PROCdelay(60,"D")
      IF damage=0 THEN
        PRINT"but no damage":defeated=0
      ELSE
        PRINT;damage;" damage..."
      ENDIF
      IF mon_str<=0 THEN
        PRINTTAB(0,mess_y+2);"...Killing the monster..."
        exp=exp+exp_gain:defeated=0:fighting=0
        IF hyman=TRUE THEN
          PROCpoke(next_x,next_y,under_mon):PROCdelay(80,"U")
        ELSE
          PROCdelay(80,"U")
        ENDIF
        ENDPROC
      ENDIF
      IF defeated=1 THEN
        PROCdelay(30,"D")
        PRINT'"The ";mon$;" staggers defeated"
      ENDIF
      PROCdelay(110,"U")
      ENDPROC
      
      DEFPROCmon_attack
      PRINTTAB(0,mess_y);"The creature attacks..."
      PROCdelay(50,"W")
      IF mon_str=0 OR (mon_str<mon_psi AND mon_psi>6 AND RND(1)<0.5) THEN
        PROCpsi_attack
      ELSE
        PROChit_attack
      ENDIF
      IF fighting=1 AND damage_factor>0 THEN
        IF mon_damage<0 THEN PRINTTAB(0,mess_y);"...Saved by your armour !  "
        IF mon_damage=0 THEN PRINTTAB(0,mess_y);"Shaken......but no damage done"
        IF mon_damage>0 THEN
          PRINTTAB(0,mess_y);"You take... ";mon_damage;" damage...          "
          con=con-mon_damage
          IF mon_damage>9 THEN
            IF damage_factor=99 THEN
              psi=INT(psi-mon_damage/4)
            ELSE
              com_str=INT(com_str-mon_damage/6)
            ENDIF
          ENDIF
          IF com_str<=0 OR con<=0 THEN ended=2:fighting=0
        ENDIF
        PROCdelay(100,"U")
      ENDIF
      ENDPROC
      
      DEFPROChit_attack
      mon_str=mon_str-1
      IF mon_str<=0 THEN
        PRINTTAB(0,mess_y);"...using its last energy in the attempt"
        exp=INT(exp+exp_gain/2):fighting=0
        IF hyman=TRUE THEN
          PROCpoke(next_x,next_y,under_mon):PROCdelay(100,"U")
        ELSE
          PROCdelay(100,"U")
        ENDIF
        ENDPROC
      ENDIF
      random=RND(10):damage_factor=0
      PRINTTAB(0,mess_y);
      CASE random OF
        WHEN 1 :PRINT"It swings at you...and misses"
        WHEN 2 :PRINT"Your blade deflects the blow"
        WHEN 3 :PRINT"...But hesitates, unsure..."
        WHEN 4 :PRINT"It strikes your head !":damage_factor=3
        WHEN 5,6 :PRINT"Your chest is struck !":damage_factor=1.5
        WHEN 7,8 :PRINT"A strike to your swordarm !":damage_factor=1
        WHEN 9 :PRINT"A blow to your body !":damage_factor=1.3
        WHEN 10: PRINT"It catches your legs !":damage_factor=0.5
      ENDCASE
      IF damage_factor=0 THEN
        PROCdelay(100,"U")
      ELSE
        PROCdelay(60,"D")
        mon_damage=INT((((mon_str*75*RND(1))-(10*com_str)-exp_factor)/100)*damage_factor)
      ENDIF
      ENDPROC
      
      DEFPROCpsi_attack
      PRINTTAB(0,mess_y);"...Hurling a lightening bolt at you !"
      mon_damage=INT(((180*mon_psi*RND(1))-(psi+exp_factor))/100)
      mon_psi=mon_psi-5
      IF mon_damage>9 THEN mon_psi=mon_psi-INT(mon_damage/5)
      PROCdelay(80,"W")
      IF mon_psi<=0 THEN
        mon_psi=0
        PRINTTAB(0,mess_y);"...using its last energy in the attempt"
        IF hyman=TRUE THEN
          PROCpoke(next_x,next_y,under_mon):PROCdelay(100,"U")
        ELSE
          PROCdelay(100,"U")
        ENDIF
        exp=INT(exp+exp_gain/2):fighting=0
        ENDPROC
      ENDIF
      IF mon_damage<=0 THEN
        mon_damage=0
        PRINTTAB(0,mess_y);"You psi shield protects you"
        PROCdelay(100,"U")
      ELSE
        PRINT TAB(0,mess_y);"It strikes home !"
        PROCdelay(110,"U"):damage_factor=99
      ENDIF
      ENDPROC
      
      DEFPROCspell_control
      PRINTTAB(0,mess_y);"Which spell seek ye ?"
      PROCcom_get
      IF got$=CHR$(255) THEN
        PRINTTAB(0,mess_y);"* Too slow...Too slow *"
        defeated=0
        PROCdelay(110,"U")
        ENDPROC
      ENDIF
      IF VAL(got$)<1 OR VAL(got$)>no_spells THEN
        PRINTTAB(0,mess_y);"No such spell..."
        PROCdelay(60,"U")
        ENDPROC
      ENDIF
      IF 4*psi*RND(1)<=mon_psi THEN
        spell_outcome=7
      ELSE
        ON VAL(got$) PROCsleep,PROCpsi_lance,PROCcrispit,PROCmagic_missile,PROCinvisible,PROCdispel_evil
      ENDIF
      CASE spell_outcome OF
        WHEN 1 :fighting=0: PROCdelay(100,"W")
          IF hyman=TRUE THEN PROCpoke(next_x,next_y,under_mon)
        WHEN 2 :PROCdelay(60,"U")
        WHEN 3 :PROCdelay(60,"U"):defeated=1
        WHEN 4 :PRINTTAB(0,mess_y);"It is beyond you     ":PROCdelay(60,"U")
        WHEN 5 :PRINTTAB(0,mess_y);"The spell saps all your strength":ended=2:fighting=0
        WHEN 6 :PRINTTAB(0,mess_y);"But the spell fails...!":PROCdelay(60,"U")
        WHEN 7:      PRINTTAB(0,mess_y);"No use, the beast'S psi shields it":PROCdelay(60,"U")
        OTHERWISE
          PRINTTAB(0,mess_y);"Something wrong with spells!":END
      ENDCASE
      ENDPROC
      
      DEFPROCsleep
      con=con-5
      IF con<=0 THEN
        spell_outcome=5
      ELSE
        PRINTTAB(0,mess_y);"Sleep you foul fiend that I may escape"'"and preserve my miserable skin"
        PROCdelay(180,"U")
        PRINTTAB(0,mess_y);"The creature staggers..."
        PROCdelay(40,"D")
        IF RND(1)<0.5 THEN
          PRINT"but recovers with a snarl !"
          spell_outcome=2
        ELSE
          PRINT"and collapses...stunned"
          spell_outcome=1
          exp=INT(exp+exp_gain/2):fighting=0
        ENDIF
      ENDIF
      ENDPROC
      
      DEFPROCpsi_lance
      IF mon_str>con OR psi<49 OR exp<1000 THEN
        spell_outcome=4
        ENDPROC
      ENDIF
      con=con-10
      IF con<=0 THEN
        spell_outcome=5
        ENDPROC
      ENDIF
      IF mon_psi=0 THEN
        PRINTTAB(0,mess_y);"This beast has no psi to attack"
        spell_outcome=2
        ENDPROC
      ENDIF
      PRINTTAB(0,mess_y);"With my mind I battle thee for my life"
      PROCdelay(120,"U")
      random=RND(1)
      IF random<0.4 AND mon_psi>10 THEN
        spell_outcome=6
        ENDPROC
      ENDIF
      damage=INT((((com_str*50*random)-5*(mon_str+mon_psi)+exp_factor)/50)/4)
      IF damage<=0 THEN damage=0:spell_outcome=7:ENDPROC
      PRINTTAB(0,mess_y);"The psi-lance causes ";damage*2;" damage"
      mon_psi=mon_psi-3*damage: IF mon_psi<=0 THEN mon_psi=0
      mon_str=mon_str-damage:IF mon_str<=0 THEN mon_str=0
      IF (mon_str+mon_psi)>0 THEN
        spell_outcome=2
      ELSE
        PRINT'"...killing the creature"
        exp=exp+exp_gain:fighting=0:spell_outcome=1
      ENDIF
      ENDPROC
      
      
      DEFPROCcrispit
      IF psi<77 OR exp<5000 THEN spell_outcome=4:ENDPROC
      con=con-20
      IF con<=0 THEN spell_outcome=5:ENDPROC
      PRINTTAB(0,mess_y);"With the might of my sword I smite thee"
      PRINT"With the power of my spell I curse thee"'"Burn ye spawn of Hell and suffer..."
      PROCdelay(240,"U")
      PRINTTAB(0,mess_y);"A bolt of energy lashes at the beast..."
      PROCdelay(80,"W")
      IF RND(1)>(psi/780)*(5-psi_gain) THEN PRINTTAB(0,mess_y);"Missed it !":spell_outcome=2:ENDPROC
      damage=INT((com_str+psi*RND(1))-(10*mon_psi*RND(1)))
      IF damage<=0 THEN damage=0:spell_outcome=7:ENDPROC
      IF mon_str=0 THEN
        mon_psi=mon_psi-damage
      ELSE
        mon_str=mon_str-damage:IF damage>10 THEN mon_psi=INT(mon_psi-(damage/3))
      ENDIF
      PRINTTAB(0,mess_y);"It strikes home causing ";damage;" damage  !"
      IF (mon_str+mon_psi)<=0 THEN
        PRINT'"The beast dies screaming !"
        exp=exp+exp_gain:fighting=0:spell_outcome=1
      ELSE
        PROCdelay(80,"D"):spell_outcome=2
      ENDIF
      ENDPROC
      
      
      DEFPROCmagic_missile
      con=con-7
      IF con<=0 THEN
        spell_outcome=5
      ELSE
        PRINTTAB(0,mess_y);"May my magic defeat thee !"
        PROCdelay(120,"W")
        IF mon_str=0 THEN
          PRINTTAB(0,mess_y);"Alas! The missile will not"'"help you here !"
          PROCdelay(90,"W")
          spell_outcome=2
        ELSE
          PRINTTAB(0,mess_y);"A magic missile speeds towards"'"the monster !"
          PROCdelay(140,"W")
          IF RND(1)>0.7 THEN
            PRINTTAB(0,mess_y);"It missed !"
            spell_outcome=2
          ELSE
            damage=INT(rating*(psi/10)-((mon_str+mon_psi)/20)+RND(5))
            PRINTTAB(0,mess_y);"The missile hits the creature..."
            IF damage<=0 THEN PRINT "...but causes no damage" ELSE PRINT"...causing ";damage;" damage"
            mon_str=mon_str-damage:spell_outcome=2
            IF mon_str<=0 THEN
              mon_str=0
              PROCdelay(95,"W")
              PRINTTAB(0,mess_y);"The beast lies dead at your feet"
              exp=exp+exp_gain:fighting=0:spell_outcome=1
            ENDIF
          ENDIF
        ENDIF
      ENDIF
      ENDPROC
      
      DEFPROCinvisible
      IF exp<3000 OR psi<60 THEN
        spell_outcome=4
      ELSE
        con=con-11
        IF con<=0 THEN
          spell_outcome=5
        ELSE
          PRINTTAB(0,mess_y);"May your eyes deceive you and"'"may I escape unharmed !"
          IF RND(1)>0.4 AND mon_psi>15 THEN
            PROCdelay(100,"W")
            spell_outcome=6
          ELSE
            PROCdantor_fx(put,what_was)
            PROCdelay(100,"W")
            PRINTTAB(0,mess_y);"The creature leaves, puzzled..."
            IF hyman=TRUE THEN PROCpoke(next_x,next_y,under_mon)
            PROCdelay(100,"W")
            PROCdantor_fx(what_was,put)
            fighting=0:spell_outcome=1
          ENDIF
        ENDIF
      ENDIF
      ENDPROC
      
      DEFPROCdispel_evil
      IF exp<4000 THEN
        spell_outcome=4
      ELSE
        IF scene=2 OR scene=3 THEN
          PRINTTAB(0,mess_y);"The forces of evil are"'"too strong here...":PROCdelay(80,"W")
          spell_outcome=2
        ELSE
          con=con-18
          IF con<=0 THEN
            spell_outcome=5
          ELSE
            PRINTTAB(0,mess_y);"With the power of my mind"'"may I dispel this evil..."
            PROCdelay(120,"W")
            IF RND(1)<0.3 THEN
              spell_outcome=6
            ELSE
              PRINTTAB(0,mess_y);"A blinding light consumes the monster"'"and then slowly fades away..."
              PROCdelay(110,"W")
              IF hyman=TRUE THEN PROCpoke(next_x,next_y,under_mon)
              fighting=0:spell_outcome=1
            ENDIF
          ENDIF
        ENDIF
      ENDIF
      ENDPROC
      
      DEFPROCdantor
      PRINTTAB(0,mess_y);SPC(119);TAB(0,mess_y);"Dantor materialises..."
      PROCdantor_fx(put,48)
      PROCdelay(30,"W")
      IF treasure=0 THEN
        PRINTTAB(0,mess_y);"Thou hast no treasure !"'"Dantor is angry !"
        PROCdelay(95,"W")
        IF con>20 THEN con=con-20
        IF com_str>10 THEN com_str=com_str-10 ELSE com_str=1
        IF psi>10 THEN psi=psi-10 ELSE psi=1
      ELSE
        IF treasure<1000 THEN
          treasure=0
          PRINTTAB(0,mess_y);"Not enough treasure for Dantor hast thou !"
          PROCdelay(90,"W")
          PRINTTAB(0,mess_y);"He steals all your treasure !"
          PROCdelay(80,"D")
        ELSE
          IF com_str<30 THEN com_str=30
          IF psi<30 THEN psi=30
          treasure=treasure-1000
          PRINTTAB(0,mess_y);"A flash of lightening strikes your body !"
          PROCdantor_fx(42,48)
          PROCdelay(20,"W")
        ENDIF
      ENDIF
      PRINTTAB(0,mess_y);"Dantor dematerialises !"
      PROCdantor_fx(48,put)
      PROCdelay(80,"U")
      ENDPROC
      
      DEFPROCdantor_fx(from,to)
      FOR I=1 TO 9
        PROCpoke(curr_x,curr_y,from)
        PROCdelay(5,"D")
        PROCpoke(curr_x,curr_y,to)
        PROCdelay(5,"D")
      NEXT I
      ENDPROC
      
      
      DEFPROCscene_control
      IF what_next=235 AND what_was=234 THEN
        PRINTTAB(0,mess_y);"You cannot enter this way..."
        turns=turns-1:con=con-10:next_x=curr_x:next_y=curr_y:what_was=234
        PROCdelay(100,"W")
        ENDPROC
      ENDIF
      FOR I=2 TO 7
        floor_data(I)=0
        REPEAT
          room_depth(I)=RND(5)+3
        UNTIL room_depth(I)<>5
      NEXT I
      IF scene=1 THEN out_x=curr_x:out_y=curr_y
      floor_data(2)=RND(30)
      temp_flag=turns
      PROCscene_draw
      ENDPROC
      
      DEFPROCscene_draw
      con=con-10
      PROCpoke(curr_x,curr_y,32):PROCpoke(next_x,next_y,put)
      IF what_next=232 THEN scene=1:floor=1
      IF what_next=240 AND scene=4 THEN scene=1:floor=1
      IF what_next=240 AND (scene=5 OR scene=6) THEN scene=scene-3:floor=floor-4:curr_x=within_x:curr_y=within_y
      IF what_next=229 THEN scene=2:floor=2
      IF what_next=228 THEN scene=3:floor=3
      IF what_next=229 OR what_next=228 THEN lake_y=RND(8):lake_x=floor_data(2)
      IF what_next=230 THEN scene=4:floor=2
      IF what_next=235 THEN scene=scene+3:floor=floor+4:within_x=curr_x:within_y=curr_y
      
      ON scene PROCvalley_draw,PROCswamp,PROCwoods,PROCtower,PROClair,PROClair
      PROCdelay(5,"U")
      ENDPROC
      
      DEFPROCscene_exit
      IF turns>temp_flag+RND(6) THEN
        PROCscene_draw
      ELSE
        PRINTTAB(0,mess_y);"The way is barred"
        turns=turns-1:con=con-10:next_x=curr_x:next_y=curr_y:
        PROCdelay(100,"W")
      ENDIF
      ENDPROC
      
      DEFPROCvalley_draw
      legal_mon$="VAEGH":floor=1:scene=1:PROCblank_screen
      FORI=1 TO 12
        PROCpoke(0,I,224):PROCpoke(38,I,224)
      NEXTI
      FORI=0 TO 38
        PROCpoke(I,0,224):PROCpoke(I,13,224)
      NEXT I
      IF path(0)=0 THEN PROCcompute_path
      FOR I=0 TO 72 STEP 2
        PROCpoke(INT(I/2)+1,path(I),path(I+1))
      NEXT I
      IF scenario_x(0)=0 THEN PROCcompute_scenarios
      FOR I=0 TO 4
        PROCpoke(scenario_x(I),scenario_y(I),scenario_c(I))
        IF I<4 THEN PROCpoke(scenario_x(I)+1,scenario_y(I),scenario_c(I))
      NEXT I
      curr_x=out_x:curr_y=out_y:next_x=curr_x:next_y=curr_y
      what_was=FNpeek(next_x,next_y):PROCpoke(curr_x,curr_y,231)
      ENDPROC
      
      DEFPROCcompute_path
      curr_x=1:curr_y=RND(11)+1
      s_index=curr_y:out_x=curr_x:out_y=curr_y:next_x=curr_x:next_y=curr_y
      path(0)=curr_y:path(1)=225
      FOR I=2 TO 72 STEP 2
        REPEAT
          IF RND(1)>0.5 THEN
            char=227
            s_index2=s_index+1
          ELSE
            char=226
            s_index2=s_index-1
          ENDIF
        UNTIL s_index2>0 AND s_index2<13
        path(I+1)=char
        IF I>2 AND path(I+1)<>path(I-1) THEN s_index2=s_index
        path(I)=s_index2:s_index=s_index2
      NEXT I
      path(73)=225
      ENDPROC
      
      DEFPROCcompute_scenarios
      FOR I=0 TO 4
        REPEAT
          y=RND(11):x=RND(34)
        UNTIL FNpeek(x,y)=32 AND FNpeek(x+1,y)=32
        scenario_x(I)=x:scenario_y(I)=y
        PROCpoke(scenario_x(I),scenario_y(I),scenario_c(I))
        IF I<4 THEN PROCpoke(scenario_x(I)+1,scenario_y(I),scenario_c(I))
      NEXT I
      scenario_c(0)=228:scenario_c(1)=228:scenario_c(2)=229:scenario_c(3)=229:scenario_c(4)=230
      ENDPROC
      
      DEFPROCblank_screen
      CLS  :PROCscreen_bottom
      
      FORI=0 TO 16:screen$(I)=STRING$(40," "):NEXT I
      ENDPROC
      
      DEFPROCswamp
      legal_mon$="AFL":char=236
      PROCdraw_swood
      ENDPROC
      
      DEFPROCwoods
      legal_mon$="FAEHLL":char=233
      PROCdraw_swood
      ENDPROC
      
      DEFPROCdraw_swood
      what_was=32
      PROCblank_screen
      FOR I=1 TO 200
        PROCpoke(RND(38),RND(13),char)
      NEXT I
      
      PROCpoke(lake_x+2,lake_y,234):PROCpoke(lake_x+3,lake_y,234)
      PROCpoke(lake_x+1,lake_y+1,234):PROCpoke(lake_x+2,lake_y+1,234):PROCpoke(lake_x+3,lake_y+1,234)
      PROCpoke(lake_x+4,lake_y+1,234):PROCpoke(lake_x+5,lake_y+1,234)
      PROCpoke(lake_x,lake_y+2,234):PROCpoke(lake_x+1,lake_y+2,234)
      PROCpoke(lake_x+2,lake_y+2,32):PROCpoke(lake_x+3,lake_y+2,32)
      PROCpoke(lake_x+4,lake_y+2,234):PROCpoke(lake_x+5,lake_y+2,234)
      PROCpoke(lake_x,lake_y+3,234):PROCpoke(lake_x+1,lake_y+3,234)
      PROCpoke(lake_x+2,lake_y+3,235):PROCpoke(lake_x+3,lake_y+3,32)
      PROCpoke(lake_x+4,lake_y+3,234):PROCpoke(lake_x+5,lake_y+3,234)
      PROCpoke(lake_x+6,lake_y+3,234)
      PROCpoke(lake_x+1,lake_y+4,234):PROCpoke(lake_x+2,lake_y+4,234)
      PROCpoke(lake_x+3,lake_y+4,234):PROCpoke(lake_x+4,lake_y+4,234)
      PROCpoke(lake_x+6,lake_y+4,234):PROCpoke(lake_x+7,lake_y+4,234)
      PROCpoke(lake_x+3,lake_y+5,234):PROCpoke(lake_x+4,lake_y+5,234)
      PROCpoke(lake_x+4,lake_y+6,234)
      FORI=0 TO 39:PROCpoke(I,0,232):PROCpoke(I,14,232):NEXT I
      FOR I=1 TO 13:PROCpoke(0,I,232):PROCpoke(39,I,232):NEXT I
      
      next_x=17:next_y=13:curr_x=17:curr_y=13:PROCpoke(next_x,next_y,32)
      IF what_next=240 THEN
        curr_x=within_x:curr_y=within_y:next_x=curr_x:next_y=curr_y
        IF bain=TRUE THEN PROCpoke(lake_x+2,lake_y+3,32)
      ENDIF
      ENDPROC
      
      DEFPROClair
      legal_mon$="CBE":P=0:depth=room_depth(floor):what_was=32:floor_data(floor)=floor_data(2)
      PROCcastle_draw
      ENDPROC
      
      DEFPROCtower
      legal_mon$="CAGE":P=0:depth=room_depth(floor):what_was=32
      PROCcastle_draw
      ENDPROC
      
      DEFPROCcastle_draw
      PROCblank_screen
      FORI=2 TO 22:PROCpoke(I,0,238):PROCpoke(I,14,238):NEXT I
      FOR I=1 TO 13:PROCpoke(2,I,238):PROCpoke(22,I,238):NEXT I
      
      wall_x=2:wall_y=1
      index=floor_data(floor)+1
      done=FALSE
      REM VERTICAL
      REPEAT
        FOR J=1 TO 3
          room_width(J)=scene_data(index):index=index+1:P=P+1
          IF room_width(J)=100 THEN index=1:room_width(J)=3:P=P+1
        NEXT J
        FOR I=0 TO depth
          char=238
          line_y=wall_y+I:line_x=wall_x
          IF line_y>13 THEN
            done=TRUE:I=depth
          ELSE
            IF I=1 THEN char=32
            IF room_width(1)=0 THEN
              char=238
            ELSE
              PROCpoke(line_x+room_width(1),line_y,char)
              char=238
            ENDIF
            IF I=3 THEN char=32
            PROCpoke(line_x+room_width(1)+room_width(2),line_y,char):char=238
            IF I=4 THEN char=32
            PROCpoke(line_x+room_width(1)+room_width(2)+room_width(3),line_y,char):char=238
          ENDIF
        NEXT I
        IF done=FALSE THEN wall_y=wall_y+depth+1
      UNTIL done
      
      REM HORIZONTAL
      wall_x=2:wall_y=1
      char=238
      FOR J=1 TO 4
        line_y=wall_y+(J*(depth+1)):line_x=wall_x
        FORK=1 TO 19
          IF line_y>12 THEN
            K=19:J=4
          ELSE
            PROCpoke(line_x+K,line_y,char)
            IF K=2 OR K=3*depth OR K=17 THEN
              PROCpoke(line_x+K,line_y,32)
              PROCpoke(line_x+K,line_y-1,32)
              PROCpoke(line_x+K,line_y+1,32)
            ENDIF
          ENDIF
        NEXT K
      NEXT J
      
      IF scene<>5 AND scene<>6 THEN
        IF floor MOD 2=0 THEN PROCpoke(3,13,239) ELSE PROCpoke(21,1,239)
      ENDIF
      IF floor=2 OR scene=5 OR scene=6 THEN PROCpoke(8,14,240):PROCpoke(8,13,32)
      IF floor_data(3)=0 THEN next_x=8: next_y=13
      PRINTFNinv;
      CASE scene OF
        WHEN 5 :PRINTTAB(25,2);"The Temple of";TAB(25,3);"  Y'Nagioth  "
        WHEN 6 :PRINTTAB(26,2);" Vounim's ";   TAB(26,3);"   Lair   "
        OTHERWISE
          PRINTFNinv;TAB(24,2);"The Black Tower";TAB(24,3);"   of Zaexon   ";TAB(24,7);"   Floor ";floor-1;FNinv
      ENDCASE
      PRINTFNinv;
      floor_data(floor+1)=floor_data(floor)+P
      IF floor_data(floor+1)>32 THEN floor_data(floor+1)=floor_data(floor+1)-32
      IF floor<4 OR RND(1)<0.3 THEN
        ENDPROC
      ELSE
        FOR I=1 TO RND(5)+1
          REPEAT
            tx=RND(19)+2
            ty=RND(12)
          UNTIL FNpeek(tx,ty)=32
          PROCpoke(tx,ty,241)
        NEXT I
      ENDIF
      ENDPROC
      
      DEFPROCstairs
      PROCpoke(next_x,next_y,231):PROCpoke(curr_x,curr_y,32)
      REPEAT
        PRINTTAB(0,mess_y);"A stairway... Up or Down ?":current_floor=floor
        valid$="UD":PROCuniget
        IF got$="U" THEN floor=floor+1 ELSE floor=floor-1
        IF floor<2 OR floor>7 THEN
          PRINTTAB(0,mess_y);"These stairs are blocked   "
          PROCdelay(60,"D")
          floor=current_floor
        ENDIF
      UNTIL floor<>current_floor
      PROCdelay(110,"D")
      con=con+10:
      PROCscene_draw
      ENDPROC
      
      DEFPROCsafe_castle
      PRINTTAB(0,mess_y);"Thou art safe in a castle"
      IF item(2)=1 THEN PROCdelay(50,"D"):PROCdramatic_end:END
      IF com_str<20 THEN com_str=20
      PROCpoke(curr_x,curr_y,what_was):what_was=FNpeek(next_x,next_y):curr_x=next_x:curr_y=next_y
      PROCpoke(curr_x,curr_y,put)
      PRINTTAB(0,mess_y);"Wilt thou leave the Valley (Y/N) ?"
      valid$="YN":PROCuniget
      PROCdelay(5,"W")
      PROCrating
      PROCdelay(110,"W")
      IF got$="Y" THEN PROCsave_char
      con=150
      PRINTTAB(0,mess_y);"Thy wounds healed...Thy sword sharp"''"Go as the Gods demand..Trust none other"
      PROCdelay(140,"U")
      ENDPROC
      
      DEFPROCsave_char
      CLS
      PRINT"Do you wish to save ";name$;" ?"
      PRINT'"Please key Y or N"
      valid$="YN":PROCuniget
      IF got$="Y" THEN spc=INSTR(name$," ") ELSE *BYE
      IF spc>0 THEN file$=LEFT$(name$,spc-1) ELSE file$=name$
      file=OPENOUT(file$)
      IF file=0 THEN
        PRINT'"Problem with save!":END
      ELSE
        PRINT#file,name$,char_type$,treasure,exp,turns,com_str,psi,com_gain,psi_gain
        FOR I=0 TO no_items
          PRINT#file,item(I)
        NEXT
        CLOSE#file
      ENDIF
      PRINT'''," *** DONE ***"
      PROCdelay(110,"D")
      *BYE
      ENDPROC
      
      DEFPROCdeath
      con=0:com_str=0:psi=0:fighting=0
      PROCdelay(120,"U")
      IF item(1)=6 THEN
        item(0)=0:item(1)=0:com_str=30:con=150:psi=30:treasure=0
        PRINTTAB(0,mess_y);"Alarian's Amulet protects thy soul"
        PRINT'FNinv;"  LIVE AGAIN  ";FNinv
        PROCdelay(150,U")
        s_index=path(0):curr_x=1:out_x=curr_x:out_y=curr_y:scene=1:PROCscene_control
        ended=0
      ELSE
        PRINTTAB(0,mess_y);" Oh what a frail shell"
        PRINT',"  Is this that we call man"
        PROCdelay(300,"W")
        A$=GET$:RUN
      ENDIF
      ENDPROC
      
      DEFPROCdramatic_ending
      CLS
      rating=FNcalc_rating
      PRINT'"I, the Wizard Alarian, pay tribute to"
      PRINT'"the skill of ";name$;" the ";char_type$
      PRINT'"Thou hast returned the Helm of Evanna"
      PRINT'"to its rightful place in her castle."
      PRINT'"   By this act, thou hast defeated"
      PRINT'"         Vounim, the Evil One"
      PRINT'" and ensured the safety of The Valley"
      PRINT'"           F O R E V E R !"
      PRINT''"    I salute you, and proclaim you"
      name$=name$+", "+rating$(rating)
      PRINT'SPC(19-(LEN(name$)DIV2));name$''
      ENDPROC
      
      
      END
      REM -----------------------------------------------------------------------------------------------------------
      
      DEFPROCinit
      no_items=3:RESTORE 1000
      DIM room_width(3),path(73),floor_data(8),room_depth(8),scenario_x(4),scenario_y(4),scenario_c(4)
      DIM item(no_items),monster$(18),mon_str(18),mon_psi(18),menu$(6)
      DIMscene_data(32),rating$(28),screen$(16),var$(5),var(5),var_exp$(5)
      ink=15:paper=0:COLOURink:COLOUR128+paper
      FORI=1 TO 32
        READ scene_data(I)
      NEXT I
      FOR I=0 TO 18
        READ monster$(I),mon_str(I),mon_psi(I)
      NEXT
      mon_graphic$="2111111021220220134"
      var$(1)="PET character set":var_exp$(1)="Use classic Commodore PET character set (Re-run programme to action)"
      var$(2)="Bain variations":var_exp$(2)="Circle of evil saps combat strength,    rating text, tufts are safe,            lairs can disappear"
      var$(3)="Dick variations":var_exp$(3)="Three extra spells, summon Dantor"
      var$(4)="Williams varation":var_exp$(4)="Ring of Power to be found"
      var$(5)="Hyman variations":var_exp$(5)="Monster graphics, no move if you retreat"
      FORI=0 TO 28
        READ rating$(I)
      NEXT I
      IF pet=-1 THEN PROCpet_def_chars ELSE PROCbbc_def_chars
      menu$(1)="Start a new game"
      menu$(2)="Load a character"
      menu$(3)="Instructions"
      menu$(4)="Variations"
      menu$(5)="History of the Valley"
      menu$(6)="Quit"
      ENDPROC
      
      DEFPROCstart_game
      REM - VARIATIONS
      REM      bain=TRUE:REM disappearing lairs, safe tufts, worse circle of evil, ending fanfare
      REM      dick=TRUE:REM extra spells, Vantor
      REM  williams=TRUE:REM ring of power
      REM     hyman=TRUE:REM monster graphics
      put=231
      valid$="":got$="":legal_mon$="":delay$=""
      treasure=0:turns=0:timing=1:fighting=0:mess_y=21
      valid_key$="123456789EeRr":put=231:old_put=231
      IF dick=TRUE THEN valid_key$="123456789EeRrDd"
      IF dick=TRUE THEN no_spells=6 ELSE no_spells=3
      
      ENDPROC
      
 1000 DATA 4,7,3,6,4,4,6,5,3,6,0,3,8,4,3,5,5,3,8,3,4,5,0,6,3,6,4,6,4,7,4,100
      DATA AWolfen,9,0,AHob-Goblin,9,0,AOrc,9,0,EFire-Imp,7,3,GRock-Troll,19,0
      DATA EHarpy,10,12,AOgre,23,0,BBarrow-Wight,0,25,HCentaur,18,14
      DATA EFire-Giant,26,20,VThunder-Lizard,50,0,CMinotaur,35,25,CWraith,0,30
      DATA FWyvern,36,12,BDragon,50,20,CRing-Wraith,0,45,ABalrog,50,50
      DATA LWater-Imp,15,15,LKraken,50,0
      
      DATA Scum,Monster Food,Peasant,Cadet,Cannon Fodder,Path Walker,Novice Adventurer,Survivor,Adventurer
      DATA Assassin,Apprentice Hero,Giant Killer,Hero,Master of the Sword,Champion,Necromancer
      DATA Loremaster,Paladin,Superhero,Dragon Slayer,Knight of the Valley,Master of Combat
      DATA Dominator,Prince of the Valley,Guardian,War Lord,Demon Killer,Lord of the Valley,Master of Destiny
      
      DEFPROCnew_char
      PROCstart_game
      CLS
      PRINT'"GENERATE NEW CHARACTER"
      PRINT'"Character's name";
      VDU 23,1,1;0;0;0;: REM enable cursor
      REPEAT
        INPUT name$
        IF LEN(name$)>16 PRINT"Too long":name$=""
        IF name$="" THEN name$="Hasty"
      UNTIL name$<>""
      VDU 23,1,0;0;0;0; : REM Disable cursor
      PRINT''"Character types... Choose carefully"'
      PRINT"Wizard    (1)"
      PRINT"Thinker   (2)"
      PRINT"Barbarian (3)","Key 1-5"
      PRINT"Warrior   (4)"
      PRINT"Cleric    (5)"
      REPEAT
        got$=GET$
      UNTIL got$<>""
      type=VAL(got$)
      CASE type OF
        WHEN 1 : char_type$="Wizard":psi_gain=2:com_gain=0.5:com_str=22:psi=28
        WHEN 2 : char_type$="Thinker":psi_gain=1.5:com_gain=0.75:com_str=24:psi=26
        WHEN 3 : char_type$="Barbarian":psi_gain=0.5:com_gain=2:com_str=28:psi=22
        WHEN 4 : char_type$="Warrior":psi_gain=1:com_gain=1.25:com_str=26:psi=24
        WHEN 5 : char_type$="Cleric":psi_gain=1.25:com_gain=1:com_str=25:psi=25
        OTHERWISE
          char_type$="Dolt":psi_gain=1:com_gain=1:com_str=20:psi=20
      ENDCASE
      exp=5:con=150
      PRINT''"Good luck"''name$;" the ";char_type$
      PROCdelay(110,"D")
      PROCvalley_draw
      PROCdelay(5,"U")
      ENDPROC
      
      DEFPROCdisk_load
      PROCstart_game
      CLS
      PRINT'"LOAD CHARACTER FROM DISK"'
      PRINT'"Character's name";
      VDU 23,1,1;0;0;0;: REM enable cursor
      REPEAT
        INPUT name$
        IF LEN(name$)>16 PRINT"Too long":name$=""
      UNTIL name$<>""
      VDU 23,1,0;0;0;0; : REM Disable cursor
      spc=INSTR(name$," ")
      IF spc>0 THEN file$=LEFT$(name$,spc-1) ELSE file$=name$
      file=OPENIN(file$)
      IF file=0 THEN
        PRINT'"No such file!":PROCdelay(150,"D"):PROCnew_char
      ELSE
        INPUT#file,name$,char_type$,treasure,exp,turns,com_str,psi,com_gain,psi_gain
        FOR I=0 TO no_items
          INPUT#file,item(I)
        NEXT
        CLOSE#file
        con=150
      ENDIF
      PRINT''"Good luck"''name$;" the ";char_type$
      PROCdelay(110,"D")
      PROCvalley_draw
      PROCdelay(5,"U")
      ENDPROC
      
      
      DEFPROCuniget
      REPEAT
        REPEAT
          got$=FNupc(GET$)
        UNTIL got$<>""
      UNTIL INSTR(valid$,got$)<>0
      ENDPROC
      
      DEF FNupc(A$)
      A%=ASC(A$)
      IF A%>=97 AND A%<=122 THEN A%=A%-32
      =CHR$(A%)
      
      DEFPROCanykey
      PRINT'"** Press any key to continue **"
      got$=GET$
      ENDPROC
      
      DEFPROCcom_get
      *FX15,0
      got$=FNupc(INKEY$(timing*200))
      PROCwipe
      ENDPROC
      
      DEFPROCwipe
      PRINTTAB(0,mess_y);SPC(40);
      ENDPROC
      
      DEFPROCdelay(delay_factor,delay_type$)
      old=TIME
      REPEAT UNTIL TIME>(old+(delay_factor*timing))
      CASE delay_type$ OF
        WHEN "W" : PRINTTAB(0,mess_y-1);SPC(159)
        WHEN "D" :
        WHEN "U" :
          PRINTTAB(0,mess_y);SPC(119);
          IF com_str>77-INT(2*psi_gain^2.5) THEN com_str=77-INT(2*psi_gain^2.5)
          IF psi<7 THEN psi=7
          IF psi>INT(42*(psi_gain+1)^LOG(psi_gain^3.7))+75 THEN psi=INT(42*(psi_gain+1)^LOG(psi_gain^3.7))+75
          IF con>125-(INT(psi_gain)*12.5) THEN con=125-INT(INT(psi_gain)*12.5)
          PRINTTAB(0,15);name$;TAB(10,15);char_type$
          PRINT"Treasure   = ";treasure'"Experience = ";exp'"Turns      = ";turns
          PRINTTAB(21,16);"Combat Str = ";com_str;"   "
          PRINTTAB(21,17);"Psi Power  = ";psi;"   "
          PRINTTAB(21,18);"Stamina    = ";con;"   "
          IF fighting THEN
            PRINTTAB(0,19);FNinv;mon$;FNinv
            PRINTTAB(21,19);"M Str =";SPC(12);TAB(29,19);mon_str;" ";mon_psi;"   ";
          ELSE
            PRINTSPC(40);
          ENDIF
      ENDCASE
      ENDPROC
      
      DEFPROCrating
      rating=FNcalc_rating
      PROCdelay(5,"W")
      PRINTTAB(0,mess_y);"Your rating now be ";rating;
      IF bain=TRUE THEN PRINT " - ";rating$(rating) ELSE PRINT
      IF item(0)=1 THEN PRINT"You have ";item(1);" amulet stone";:IF item(1)<>1 THEN PRINT"s"
      IF item(3)=1 THEN PRINT"You have the Ring of Power"
      IF item(2)=1 THEN PRINT"You have the Helm of Evanna"
      PROCdelay(250,"W")
      IF got$="E" THEN con=con-10:got$="":REM check!
      ENDPROC
      
      DEFFNcalc_rating
      LOCAL R
      R=INT(0.067*(exp+treasure/3)^0.5+LOG(exp/((turns+1)^1.5)))
      IF R>28 THEN R=28
      IF R<0 THEN R=0
      =R
      
      DEFFNpeek(X%,Y%)=ASC(MID$(screen$(Y%),X%+1,1))
      
      DEFPROCpoke(X%,Y%,C%)
      MID$(screen$(Y%),X%+1,1)=CHR$(C%)
      VDU31,X%,Y%,C%
      ENDPROC
      
      DEF FNinv
      SWAP ink,paper
      COLOURink:COLOURpaper+128
      =""
      
      DEFPROCpet_def_chars
      REM PET GRAPHIC CHARS FOR VALLEY
      VDU23,224,126,189,219,231,231,219,189,126
      VDU23,225,247,247,247,247,0,247,247,247
      VDU23,226,1,2,4,8,16,32,64,128
      VDU23,227,128,64,32,16,8,4,2,1
      VDU23,228,247,227,213,136,213,247,247,255
      VDU23,229,255,255,255,129,255,255,255,255
      VDU23,230,0,60,66,66,66,66,60,0
      VDU23,231,0,60,126,126,126,126,60,0
      VDU23,232,0,0,0,0,0,0,0,0
      VDU23,233,8,28,42,119,42,8,8,0
      VDU23,234,255,255,255,255,255,255,255,255
      VDU23,235,85,170,85,170,85,170,85,170
      VDU23,236,0,0,0,126,0,0,0,0
      VDU23,237,255,195,129,129,129,129,195,255
      VDU23,237,255,195,129,129,129,129,195,255
      VDU23,238,255,255,255,255,255,255,255,255
      VDU23,239,170,85,170,85,170,85,170,85
      VDU23,240,0,0,0,0,170,85,170,85
      VDU23,241,8,42,28,62,28,42,8,0
      
      VDU23,242,8,28,62,127,127,28,62,0
      VDU23,243,8,28,62,127,62,28,8,0
      VDU23,244,0,0,2,124,168,40,40,0
      
      VDU23,245,247,227,193,128,193,227,247,255
      VDU23,246,255,255,253,131,87,215,215,255
      
      REM PET ALPHANUMERICS
      VDU23,65,24,36,66,126,66,66,66,0
      VDU23,66,124,34,34,60,34,34,124,0
      VDU23,67,28,34,64,64,64,34,28,0
      VDU23,68,120,36,34,34,34,36,120,0
      VDU23,69,126,64,64,120,64,64,126,0
      VDU23,70,126,64,64,120,64,64,64,0
      VDU23,71,28,34,64,78,66,34,28,0
      VDU23,72,66,66,66,126,66,66,66,0
      VDU23,73,28,8,8,8,8,8,28,0
      VDU23,74,14,4,4,4,4,68,56,0
      VDU23,75,66,68,72,112,72,68,66,0
      VDU23,76,64,64,64,64,64,64,126,0
      VDU23,77,66,102,90,90,66,66,66,0
      VDU23,78,66,98,82,74,70,66,66,0
      VDU23,79,24,36,66,66,66,36,24,0
      VDU23,80,124,66,66,124,64,64,64,0
      VDU23,81,24,36,66,66,74,36,26,0
      VDU23,82,124,66,66,124,72,68,66,0
      VDU23,83,60,66,64,60,2,66,60,0
      VDU23,84,62,8,8,8,8,8,8,0
      VDU23,85,66,66,66,66,66,66,60,0
      VDU23,86,66,66,66,36,36,24,24,0
      VDU23,87,66,66,66,90,90,102,66,0
      VDU23,88,66,66,36,24,36,66,66,0
      VDU23,89,34,34,34,28,8,8,8,0
      VDU23,90,126,2,4,24,32,64,126,0
      VDU23,33,8,8,8,8,0,0,8,0
      VDU23,34,36,36,36,0,0,0,0,0
      VDU23,35,36,36,126,36,126,36,36,0
      VDU23,36,16,60,80,56,20,120,16,0
      VDU23,37,0,98,100,8,16,38,70,0
      VDU23,38,48,72,72,48,74,68,58,0
      VDU23,39,4,8,16,0,0,0,0,0
      VDU23,40,4,8,16,16,16,8,4,0
      VDU23,41,32,16,8,8,8,16,32,0
      VDU23,42,8,42,28,62,28,42,8,0
      VDU23,43,0,8,8,62,8,8,0,0
      VDU23,44,0,0,0,0,0,8,8,16
      VDU23,45,0,0,0,126,0,0,0,0
      VDU23,46,0,0,0,0,0,24,24,0
      VDU23,47,0,2,4,8,16,32,64,0
      VDU23,48,60,66,70,90,98,66,60,0
      VDU23,49,8,24,40,8,8,8,62,0
      VDU23,50,60,66,2,12,48,64,126,0
      VDU23,51,60,66,2,28,2,66,60,0
      VDU23,52,4,12,20,36,126,4,4,0
      VDU23,53,126,64,120,4,2,68,56,0
      VDU23,54,28,32,64,124,66,66,60,0
      VDU23,55,126,66,4,8,16,16,16,0
      VDU23,56,60,66,66,60,66,66,60,0
      VDU23,57,60,66,66,62,2,4,56,0
      VDU23,58,0,0,8,0,0,8,0,0
      VDU23,59,0,0,8,0,0,8,8,16
      VDU23,60,14,24,48,96,48,24,14,0
      VDU23,61,0,0,126,0,126,0,0,0
      VDU23,62,112,24,12,6,12,24,112,0
      VDU23,63,60,66,2,12,16,0,16,0
      VDU23,64,28,34,74,86,76,32,30,0
      REM lower case gone!
      VDU23,97,24,36,66,126,66,66,66,0
      VDU23,98,124,34,34,60,34,34,124,0
      VDU23,99,28,34,64,64,64,34,28,0
      VDU23,100,120,36,34,34,34,36,120,0
      VDU23,101,126,64,64,120,64,64,126,0
      VDU23,102,126,64,64,120,64,64,64,0
      VDU23,103,28,34,64,78,66,34,28,0
      VDU23,104,66,66,66,126,66,66,66,0
      VDU23,105,28,8,8,8,8,8,28,0
      VDU23,106,14,4,4,4,4,68,56,0
      VDU23,107,66,68,72,112,72,68,66,0
      VDU23,108,64,64,64,64,64,64,126,0
      VDU23,109,66,102,90,90,66,66,66,0
      VDU23,110,66,98,82,74,70,66,66,0
      VDU23,111,24,36,66,66,66,36,24,0
      VDU23,112,124,66,66,124,64,64,64,0
      VDU23,113,24,36,66,66,74,36,26,0
      VDU23,114,124,66,66,124,72,68,66,0
      VDU23,115,60,66,64,60,2,66,60,0
      VDU23,116,62,8,8,8,8,8,8,0
      VDU23,117,66,66,66,66,66,66,60,0
      VDU23,118,66,66,66,36,36,24,24,0
      VDU23,119,66,66,66,90,90,102,66,0
      VDU23,120,66,66,36,24,36,66,66,0
      VDU23,121,34,34,34,28,8,8,8,0
      VDU23,122,126,2,4,24,32,64,126,0
      
      VDU23,91,60,32,32,32,32,32,60,0
      VDU23,92,0,64,32,16,8,4,2,0
      VDU23,93,60,4,4,4,4,4,60,0
      VDU23,94,0,8,28,42,8,8,8,0
      VDU23,95,0,0,16,32,126,32,16,0
      ENDPROC
      
      DEFPROCbbc_def_chars
      VDU23,224,126,189,219,231,231,219,189,126
      VDU23,225,231,231,231,0,0,231,231,231
      VDU23,226,1,2,4,8,16,32,64,128
      VDU23,227,128,64,32,16,8,4,2,1
      VDU23,228,255,193,128,128,128,148,247,247
      VDU23,230,255,231,219,189,189,219,231,255
      VDU23,231,24,24,65,90,88,64,36,36
      VDU23,239,0,1,3,7,15,31,63,127
      VDU23,235,170,85,170,85,170,85,170,85
      VDU23,229,255,213,128,255,255,171,1,255
      VDU23,233,0,62,127,127,127,107,8,8
      VDU23,238,255,255,255,255,255,255,255,255
      VDU23,236,2,5,32,80,4,10,64,160
      VDU23,240,129,189,189,181,189,189,189,129
      VDU23,234,255,204,51,255,255,204,51,255
      VDU23,241,153,90,36,219,219,36,90,153
      VDU23,232,255,129,129,129,129,129,129,255
      VDU23,237,231,231,190,165,167,191,219,219
      
      VDU23,242,0,16,55,124,84,124,124,254
      VDU23,243,28,28,62,127,93,93,85,20
      VDU23,244,0,0,111,236,62,62,34,34
      VDU23,245,227,227,193,128,162,162,170,235
      VDU23,246,255,255,144,19,193,193,221,221
      ENDPROC
      
      DEFPROCscreen_bottom
      PRINTTAB(0,25);STRING$(39,"-")
      PRINTTAB(0,26);"      789"
      PRINTTAB(0,27);"Move: 4";CHR$(231);"6"
      PRINTTAB(0,28);"      123"
      PRINTTAB(0,30);"(R) Rating"
      IF dick=TRUE THEN PRINT"(D) Dantor";
      
      IF scene=1 THEN
        PRINTTAB(11,26);CHR$(225);"  = Safe Castle  "
        PRINTTAB(28,26);CHR$(226);CHR$(227);" = Path"
        PRINTTAB(11,28);CHR$(228);CHR$(228);" = Woods"
        PRINTTAB(28,28);CHR$(229);CHR$(229);" = Swamp"
        PRINTTAB(11,30);CHR$(230);"  = Tower of Zaexon"
      ENDIF
      
      
      IF scene=2 THEN
        PRINTTAB(11,26);CHR$(236);" = Tuft"
        PRINTTAB(20,26);CHR$(234);" = Lake"
        PRINTTAB(11,28);CHR$(235);" = Temple of Y'Nagioth"
      ENDIF
      
      IF scene=3 THEN
        PRINTTAB(11,26);CHR$(233);" = Tree"
        PRINTTAB(20,26);CHR$(234);" = Lake"
        PRINTTAB(11,28);CHR$(235);" = Vounim's Lair"
      ENDIF
      
      IF scene>3 THEN
        PRINTTAB(10,26);CHR$(238);" = Wall"
        PRINTTAB(19,26);CHR$(239);" = Stairs"
        PRINTTAB(10,28);CHR$(240);" = Door"
        PRINTTAB(19,28);CHR$(241);" = Possible treasure"
      ENDIF
      ENDPROC
      
      DEF PROCmain_menu
      PRINTTAB(14,1)"THE VALLEY"
      PRINTTAB(14,2)"=========="
      PRINTTAB(0,4)"Adapted by Fraser Charlton August 2004"
      PRINTTAB(0,5)"using BBC Basic for Windows"
      RESTORE
      FOR menu=1 TO 6
        PRINTTAB(10,6+menu*2);FNinv;LEFT$(menu$(menu),1);FNinv;MID$(menu$(menu),2)
      NEXT
      
      
      menu$="SLIVHQ"
      REPEAT
        got$=FNupc(GET$)
        choice=INSTR(menu$,got$)
      UNTIL choice<>0
      ON choice PROCnew_char,PROCdisk_load,PROCwrapper("INSTRUCTIONS",3000,40,25),PROCvariations,PROCwrapper("HISTORY OF THE VALLEY",2000,40,25),PROCbye
      ENDPROC
      
      DEFPROCbye
      *QUIT
      ENDPROC
      DEFPROCload_prefs
      in=OPENIN("valley_prefs")
      IF in=0 THEN
        out=OPENOUT("valley_prefs")
        PRINT#out,0,0,0,0,0
        CLOSE#out
        in=OPENIN("valley_prefs")
      ENDIF
      INPUT#in,pet,bain,dick,williams,hyman
      CLOSE#in
      ENDPROC
      
      DEFPROCsave_prefs
      out=OPENOUT("valley_prefs")
      PRINT#out,pet,bain,dick,williams,hyman
      CLOSE#out
      ENDPROC
      
      
      DEFPROCvariations
      REPEAT
        CLS
        PROCname_to_var
        PRINT'"VALLEY VARIATIONS"'
        FOR menu=1 TO 5
          PRINTFNinv;menu;") ";var$(menu);" - ";
          IF var(menu)=-1 THEN PRINT"ON" ELSE PRINT"OFF"
          PRINTFNinv;var_exp$(menu)'
        NEXT menu
        PRINTFNinv;"6) - Save changes";FNinv''"Hit number to toggle variation ON or OFF"
        REPEAT
          got=GET-48
        UNTIL got>0 AND got<7
        IF got<6 THEN
          IF var(got)=-1 THEN var(got)=0 ELSE var(got)=-1
          PROCvar_to_name
        ELSE
          PROCsave_prefs:RUN
        ENDIF
      UNTIL got=6
      ENDPROC
      
      DEFPROCvar_to_name
      pet=var(1)
      bain=var(2)
      dick=var(3)
      williams=var(4)
      hyman=var(5)
      ENDPROC
      
      DEFPROCname_to_var
      var(1)=pet
      var(2)=bain
      var(3)=dick
      var(4)=williams
      var(5)=hyman
      ENDPROC
      
      DEFPROCwrapper(title$,start_line,columns,rows)
      REM MODE 16:COLOUR 3:COLOUR132
      CLS:RESTORE start_line
      centre=(columns-LEN(title$))DIV2
      PRINT'TAB(centre);title$
      PRINTTAB(centre);STRING$(LEN(title$),"=")'
      REPEAT
        READ text$
        IF text$="/" THEN PRINT'
        IF text$="/p" THEN PRINT''"Press any key to continue";:A$=GET$:CLS:PRINT:text$="/"
        IF text$<>"/" AND text$<>"*end*" THEN PROCword_wrap(text$,40,25)
      UNTIL text$="*end*"
      PRINT''"Press any key to continue";:A$=GET$
      RUN
      ENDPROC
      
 2000 DATA"'The Valley' was a type-in game listing first published in the magazine 'Computing Today' in April 1982"
      DATA"It was presented in such a way that, although written for the Commodore PET, it could be easily adapted"
      DATA"for other systems - the listing was broken down into modules, with notes indicating the strucure of the code, graphics, variables, etc."
      DATA"The magazine sold versions for many machines, including the TRS-80 and BBC Model B (see scanned pages).","/"
      DATA"It turned out to be very popular, including with my friends and me, and we spent many happy hours converting it to our systems and playing it -"
      DATA"it really was an *excellent* game.","/p"
      DATA"That wasn't the end of it, though - there was a further article in May 1982 about converting the programme"
      DATA"to run on a Tandy TRS-80, and two articles of 'Vally Variations' in August 1982 and April 1983 (see scans)."
      DATA"Many of the variations were excellent, greatly increasing your fun.","/","You can choose which variations"
      DATA"you wish to incorporate into your game via the 'Variations' screen - they are named after the person who sent them in, incidentally.","/"
      DATA"I adapted the programme myself for the BBC Micro B, Psion 3a and, most recently, BBC Basic for Windows."
      DATA"Basically (pun intended), I have used it as a way of getting acquainted with a new computer language.","/p"
      DATA"To maximise the 'retro' nature of this game, you can run it with the original"
      DATA"PET character set and graphics, or with the BBC Micro user-defined characters from the published version - change the setting"
      DATA"on the 'Variations' screen.","/","The only other visual change is that there is now an aide memoire at the bottom of the screen to remind you"
      DATA"which keys to press, and what the symbols mean - this is based on the commercial BBC version.","/"
      DATA"So, you can enjoy my definitive version of 'The Valley' in its original PET glory,","or maxed out with (slightly) fancy graphics and interesting variations!"
      DATA"/","Strap on your armour, memorise your spells and prepare to enter... The Valley!","/"
      DATA"*end*"
      
 3000 DATA "For a full introduction to this game, read the scanned pages from 'Computing Today' bundled with the game."
      DATA "Briefly, you must choose a name and a profession for your character (the more magical professions take longer to advance,"
      DATA "but are ultimately the most powerful). You will then find yourself in the Valley. There are two safe castles and a safe path"
      DATA "between them, and you move about with the numbers around '5' on the numeric keypad. The swamps, tower and forest give access to new screens.","/"
      DATA "Your mission is as follows: first, find the Amulet of Alarian, which is in a Temple of Y'Nagioth in one of the swamps."
      DATA "Next, find the six stones to fill the Amulet in the Black Tower of Zaexon - this will resurrect you if you are killed!","/p"
      DATA "Finally, find the Helm of Evanna, in Vounim's Lair in one of the woods, and bring it to a safe castle. You must have a Rating of at least 26 to find this.","/"
      DATA "You will encounter monsters during your travels, which you can fight with sword (choose to aim at the Head, Body or Limbs) or magic."
      DATA "There are 3 spells in the basic game: 1) Sleep, 2) Psi Lance & 3) Crispit - they have increasing Psi Power requirements."
      DATA "You die if your Stamina reaches 0. Both Combat Strength and Psi Power increase when you encounter good places."
      DATA "Finally, you can be healed and save the game when you reach a safe castle (you must 'Quit' before you can save).","/p"
      DATA "There are a few variations you can choose to have, taken from updates published in 'Computing Today':","/"
      DATA "The Bain Variation makes tufts in the swamps safe places, lairs disappear once you leave them, stop circles of evil from increasing your Psi.","/"
      DATA "The Dick Variation gives you three extra spells: 4) Magic Missile, 5) Invisibility and 6) Dispel Evil. You can also 'Summon Dantor' by pressing 'D' -"
      DATA "he will restore your stamina at the cost of treasure!","/"
      DATA "The Williams Variation gives a Ring of Power that you can find in swamp temples after you've found the amulet.","/"
      DATA "The Hyman Variation gives a graphic character to represent creatures, and won't allow you to move if you retreat from a combat.","/"
      DATA "You'll learn more as you explore - have fun!","*end*"
      
      
      
      DEFPROCword_wrap(a$,line,height)
      LOCAL word$
      REPEAT
        word$=LEFT$(a$,INSTR(a$," ")-1)
        REM PRINTa$,word$
        IF (POS+LEN(word$)+1)>line THEN PRINT:IF VPOS>(height) THEN PRINT'"Press any key to continue";:A$=GET$:CLS
        
        PRINT word$;" ";
        IF a$<>word$ THEN a$=RIGHT$(a$,LEN(a$)-LEN(word$)-1) ELSE a$=""
        REM PRINT'a$:A$=GET$
      UNTIL a$=""
      ENDPROC
